package top.wintp.offlinedataanalysis.anlyser.dim;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

import top.wintp.offlinedataanalysis.anlyser.dim.base.BaseDimension;
import top.wintp.offlinedataanalysis.anlyser.dim.base.DateDimension;
import top.wintp.offlinedataanalysis.anlyser.dim.base.KpiDimension;
import top.wintp.offlinedataanalysis.anlyser.dim.base.PlatformDimension;


/**
 * 公用的dimension信息组合
 *
 * @author root
 */
public class StatsCommonDimension extends StatsDimension {
    private DateDimension date = new DateDimension();
    private PlatformDimension platform = new PlatformDimension();
    private KpiDimension kpi = new KpiDimension();

    /**
     * clone 一个对象
     *
     * @param dimension
     * @return
     */
    public static StatsCommonDimension clone(StatsCommonDimension dimension) {

        DateDimension date = new DateDimension(dimension.date.getId(), dimension.date.getYear(),
                dimension.date.getSeason(), dimension.date.getMonth(), dimension.date.getWeek(),
                dimension.date.getDay(), dimension.date.getType(), dimension.date.getCalendar());

        PlatformDimension platform = new PlatformDimension(dimension.platform.getId(),
                dimension.platform.getPlatformName());

        KpiDimension kpi = new KpiDimension(dimension.kpi.getId(), dimension.kpi.getKpiName());

        return new StatsCommonDimension(date, platform, kpi);
    }

    public StatsCommonDimension() {
        super();
    }

    public StatsCommonDimension(DateDimension date, PlatformDimension platform, KpiDimension kpi) {
        super();
        this.date = date;
        this.platform = platform;
        this.kpi = kpi;
    }

    public DateDimension getDate() {
        return date;
    }

    public void setDate(DateDimension date) {
        this.date = date;
    }

    public PlatformDimension getPlatform() {
        return platform;
    }

    public void setPlatform(PlatformDimension platform) {
        this.platform = platform;
    }

    public KpiDimension getKpi() {
        return kpi;
    }

    public void setKpi(KpiDimension kpi) {
        this.kpi = kpi;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        this.date.write(out);
        this.platform.write(out);
        this.kpi.write(out);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.date.readFields(in);
        this.platform.readFields(in);
        this.kpi.readFields(in);
    }

    @Override
    public int compareTo(BaseDimension o) {
        if (this == o) {
            return 0;
        }

        StatsCommonDimension other = (StatsCommonDimension) o;
        int tmp = this.date.compareTo(other.date);
        if (tmp != 0) {
            return tmp;
        }
        tmp = this.platform.compareTo(other.platform);
        if (tmp != 0) {
            return tmp;
        }
        tmp = this.kpi.compareTo(other.kpi);
        return tmp;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        StatsCommonDimension that = (StatsCommonDimension) o;
        return Objects.equals(date, that.date) &&
                Objects.equals(platform, that.platform) &&
                Objects.equals(kpi, that.kpi);
    }

    @Override
    public int hashCode() {

        return Objects.hash(date, platform, kpi);
    }

}
